package com.hunter.jerrymouse.request.netty;

import com.hunter.jerrymouse.request.MultiPartFileRequest;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.multipart.FileUpload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by hunter
 * <p>
 * netty实现的文件请求实现类
 */
public class NettyFileRequest implements MultiPartFileRequest {

    /**
     * 持有的代理类
     */
    private FileUpload fileUpload;

    NettyFileRequest(FileUpload fileUpload) {
        this.fileUpload = fileUpload;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return fileUpload.get();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteBufInputStream(Unpooled.wrappedBuffer(getBytes()));
    }

    @Override
    public void transferTo(File outFile) throws IOException {
        fileUpload.renameTo(outFile);
    }

    @Override
    public String getFileName() {
        return fileUpload.getFilename();
    }
}
